package com.fingard.dsp.bank.directbank.lsbank01;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XmlTextReader;

/**
 * Created by aaron on 2018/4/9.
 */
public class LSBANK01SinglePay extends LSBANK01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        return p_req1908.singleDetail.sameBnk.equals("1")?payBetweenSameBanks(p_req1908):payBetweenDifferenceBanks(p_req1908);
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String opname = "otherAccQry";
        try {
            busDateTime14 = DateFormatUtils.format(p_req1944.getBgnDateTime(), "yyyyMMddHHmmss");
        }catch (ParseException e){
            throw new RuntimeException(e);
        }
        Map<String,String> param = new HashMap<String, String>();
        try{
            param.put("orderFlowNo",generateSerialNo(p_req1944.reqHeader));
            String[] bankResult = sendToBank(param,p_req1944.reqHeader,opname);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(xmlSbReader!=null){
                    String hostReturnCode = getHostReturnCode(xmlSbReader);
                    if(isSuccess(hostReturnCode)){
                        detail.respCode = "0";
                        detail.respInfo = xmlSbReader.getFirstText("/BankData/opRep/RepParam/errorMessage");
                        detail.reqSeqID = p_req1944.askNumber;
                        detail.payInfoCode = xmlSbReader.getFirstText("/BankData/opRep/RepParam/state");
                        detail.transState = hostReturnCode + "+"+detail.payInfoCode;
                    }else if(!StringHelper.isNullOrEmpty(hostReturnCode)){
                        detail.respCode = "0";
                        detail.respInfo = xmlSbReader.getFirstText("/BankData/opRep/hostErrorMessage");                        detail.reqSeqID = p_req1944.askNumber;
                        detail.transState = hostReturnCode;
                        detail.payInfoCode = detail.transState;
                        detail.reqSeqID = p_req1944.askNumber;
                    }
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }

        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
    private RetAtsSingle payBetweenSameBanks(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        Map<String,String> repParams = new HashMap<String, String>();
        String opname = "enterpriseAccPay";
        String serialNO = generateSerialNo(p_req1908.reqHeader);
        try{
            repParams.put("payAccount",p_req1908.singleDetail.payAct);
            repParams.put("payAccountName",p_req1908.singleDetail.payName);
            repParams.put("recAccount",p_req1908.singleDetail.recAct);
            repParams.put("recAccountName",p_req1908.singleDetail.recName);
            repParams.put("payAmount",p_req1908.singleDetail.payAmount);
            repParams.put("currencyType",convertCurToBank(p_req1908.singleDetail.payCur));
            repParams.put("orderFlowNo",serialNO);
            repParams.put("payUse",p_req1908.singleDetail.purpose);
            repParams.put("trsDateTime",getCommonReqDate(p_req1908.reqHeader));
            String[] bankResult = sendToBank(repParams,p_req1908.reqHeader,opname);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg =new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String hostReturnCode = getHostReturnCode(tmpBkReader);
                    if(isSuccess(hostReturnCode)){
                        detail.respCode = "0";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.reqSeqID =reqHeader.reqSeqID;
                        detail.outTransID = serialNO;
                        detail.transState = hostReturnCode;
                        detail.payInfoCode = detail.transState;
                    }else if(hostReturnCode.matches("9+")){
                        detail.respCode = "-1";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.reqSeqID =reqHeader.reqSeqID;
                        detail.outTransID = serialNO;
                    }else{
                        detail.respCode = "0";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.reqSeqID =reqHeader.reqSeqID;
                        detail.outTransID = serialNO;
                        detail.transState = hostReturnCode;
                        detail.payInfoCode = detail.transState;
                    }
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                    detail.outTransID = serialNO;
                }
            } else if(bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transAmt = p_req1908.singleDetail.payAmount;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else{
                detail.respCode="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode="-1";
            detail.respInfo=e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    private RetAtsSingle payBetweenDifferenceBanks(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String opname = "otherAccPay";
        HashMap<String,String> opParam = new HashMap<String, String>();
        try{
            opParam.put("payAccount",p_req1908.singleDetail.payAct);
            opParam.put("payAccountName",p_req1908.singleDetail.payName);
            opParam.put("recAccount",p_req1908.singleDetail.recAct);
            opParam.put("recAccountName",p_req1908.singleDetail.recName);
            opParam.put("payAmount",p_req1908.singleDetail.payAmount);
            String remRoute;
            if(p_req1908.singleDetail.isLargeAmt()||"1".equals(p_req1908.singleDetail.paySpd)){
                remRoute = "5";
            }else{
                remRoute = "7";
            }
            opParam.put("remRoute",remRoute);
            opParam.put("currencyType",convertCurToBank(p_req1908.singleDetail.payCur));
            opParam.put("recAccountOpenBank",p_req1908.singleDetail.cnapsCode);
            opParam.put("recAccountOpenBankName",p_req1908.singleDetail.cnapsName);
            String serialNo = generateSerialNo(reqHeader);
            opParam.put("orderFlowNo",serialNo);
            opParam.put("payUse",p_req1908.singleDetail.purpose);
            opParam.put("trsDateTime",getCommonReqDate(reqHeader));
            String[] bankResult = sendToBank(opParam,p_req1908.reqHeader,opname);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String hostReturnCode  = getHostReturnCode(tmpBkReader);
                    if(isSuccess(hostReturnCode)){
                        detail.respCode = "0";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.reqSeqID = p_req1908.singleDetail.askNumber;
                        detail.outTransID = serialNo;
                        detail.transState = hostReturnCode;
                        detail.payInfoCode = detail.transState;
                    }else if(hostReturnCode.matches("9+")){
                        detail.respCode = "0";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.reqSeqID = p_req1908.singleDetail.askNumber;
                        detail.outTransID = serialNo;
                        detail.transState = "999";
                        detail.payInfoCode = detail.transState;
                    }else if(!StringHelper.isNullOrEmpty(hostReturnCode)){
                        detail.respCode = "0";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.reqSeqID = p_req1908.singleDetail.askNumber;
                        detail.outTransID = serialNo;
                        detail.transState = hostReturnCode;
                        detail.payInfoCode = detail.transState;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = getHostErrorMessage(tmpBkReader);
                        detail.outTransID = serialNo;
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                    detail.outTransID = serialNo;
                }
            }else if(bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transAmt = p_req1908.singleDetail.payAmount;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            } else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode ="-1";
            detail.respInfo = e.getMessage();
            e.getStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

}
